package com.aluka.nirvana.framework.security.handler;

import com.aluka.nirvana.framework.security.configuration.SecurityCustomsConfiguration;
import com.aluka.nirvana.framework.security.constant.Constants;
import com.aluka.nirvana.framework.security.principal.AuthenticationSuccessResponse;
import com.aluka.nirvana.framework.security.principal.BaseUserDetails;
import com.aluka.nirvana.framework.security.utils.JwtUtils;
import com.aluka.nirvana.framework.security.utils.ResponseUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;

/**
 * 自定义 Security 登陆成功处理类
 * @author gongli
 * @since 2019/4/10 13:40
 */
@Component
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private SecurityCustomsConfiguration customsConfiguration;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication) throws IOException, ServletException {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, customsConfiguration.getTokenExpire());
        // 创建 Json Web Token
        BaseUserDetails userDetails = (BaseUserDetails) authentication.getPrincipal();
        String loginSuccessCustomFields = customsConfiguration.getLoginSuccessCustomFields();
        String token = JwtUtils.create(userDetails, calendar.getTime(), loginSuccessCustomFields);
        // 封装返回数据
        AuthenticationSuccessResponse response = new AuthenticationSuccessResponse();
        response.setStatus(HttpStatus.OK.value());
        response.setMessage("登录成功!");
        response.setToken(Constants.TOKEN_PREFIX + token);
        response.setScope(userDetails.getAuthorities());
        response.setExpire(calendar.getTime().getTime());
        response.setCustomField(bindCustomField(userDetails));

        ResponseUtils.writeResponse(httpServletResponse, response);
    }

    private Map<String, Object> bindCustomField(BaseUserDetails userDetails) {
        Map<String, Object> customFields = Maps.newHashMap();
        String loginSuccessCustomFields = customsConfiguration.getLoginSuccessCustomFields();
        for (String customField : StringUtils.split(loginSuccessCustomFields, ",")) {
            Object customFieldValue = userDetails.getCustomField(customField);
            if(customFieldValue != null){
                customFields.put(customField, customFieldValue);
            }
        }
        return customFields;
    }
}
